package com.woniuxy.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.entity.ResponseResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json");
        httpServletResponse.setCharacterEncoding("utf-8");

        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.writeValue(httpServletResponse.getWriter(), new ResponseResult(401,null,e.getMessage()));
    }
}
